home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / wind2.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  16KB  |  483 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glx.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <stdlib.h>
  43. #include <X11/keysym.h>
  44.  
  45. static int RGB_attributes_DB[] = {
  46.     GLX_RGBA,
  47.     GLX_RED_SIZE, 1,
  48.     GLX_GREEN_SIZE, 1,
  49.     GLX_BLUE_SIZE, 1,
  50.     GLX_DOUBLEBUFFER,
  51.     None,
  52. };
  53.  
  54. static int CI_attributes_DB[] = {
  55.     GLX_DOUBLEBUFFER,
  56.     None,
  57. };
  58.  
  59. static int RGB_attributes[] = {
  60.     GLX_RGBA,
  61.     GLX_RED_SIZE, 1,
  62.     GLX_GREEN_SIZE, 1,
  63.     GLX_BLUE_SIZE, 1,
  64.     None,
  65. };
  66.  
  67. static int CI_attributes[] = {
  68.     None,
  69. };
  70.  
  71. int rgb = 0;
  72. int W = 300;
  73. int H = 300;
  74. int doubleBuf = 1;
  75. GLXContext cx, cx2;
  76. Window window, window2;
  77. Display *dpy;
  78.  
  79. unsigned char font6x10[] = {
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  81. 0x00, 0x00, 0x10, 0x38, 0x38, 0x7c, 0x38, 0x38, 0x10, 0x00, 
  82. 0x00, 0x00, 0x54, 0x28, 0x54, 0x28, 0x54, 0x28, 0x54, 0x00, 
  83. 0x08, 0x08, 0x08, 0x3c, 0x48, 0x48, 0x78, 0x48, 0x48, 0x00, 
  84. 0x10, 0x10, 0x18, 0x10, 0x5c, 0x40, 0x60, 0x40, 0x70, 0x00, 
  85. 0x24, 0x24, 0x38, 0x24, 0x38, 0x38, 0x40, 0x40, 0x38, 0x00, 
  86. 0x20, 0x20, 0x38, 0x20, 0x3c, 0x78, 0x40, 0x40, 0x40, 0x00, 
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x28, 0x28, 0x38, 0x00, 
  88. 0x00, 0x00, 0x7c, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00, 0x00, 
  89. 0x3c, 0x20, 0x20, 0x20, 0x48, 0x58, 0x68, 0x68, 0x48, 0x00, 
  90. 0x08, 0x08, 0x08, 0x08, 0x3c, 0x20, 0x30, 0x48, 0x48, 0x00, 
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x10, 0x10, 0x10, 0x10, 
  92. 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, 0x00, 0x00, 0x00, 0x00, 
  93. 0x10, 0x10, 0x10, 0x10, 0x10, 0x1c, 0x00, 0x00, 0x00, 0x00, 
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x10, 0x10, 0x10, 0x10, 
  95. 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x10, 0x10, 
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  99. 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  100. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  101. 0x10, 0x10, 0x10, 0x10, 0x10, 0x1c, 0x10, 0x10, 0x10, 0x10, 
  102. 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, 0x10, 0x10, 0x10, 0x10, 
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 
  104. 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  105. 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 
  106. 0x00, 0x7c, 0x04, 0x08, 0x20, 0x40, 0x20, 0x08, 0x04, 0x00, 
  107. 0x00, 0x7c, 0x40, 0x20, 0x08, 0x04, 0x08, 0x20, 0x40, 0x00, 
  108. 0x00, 0x00, 0x28, 0x28, 0x28, 0x28, 0x7c, 0x00, 0x00, 0x00, 
  109. 0x00, 0x00, 0x40, 0x20, 0x7c, 0x10, 0x7c, 0x08, 0x04, 0x00, 
  110. 0x00, 0x20, 0x6c, 0x38, 0x20, 0x78, 0x20, 0x24, 0x18, 0x00, 
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  113. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x28, 0x28, 0x00, 
  115. 0x00, 0x00, 0x28, 0x28, 0x7c, 0x28, 0x7c, 0x28, 0x28, 0x00, 
  116. 0x00, 0x00, 0x10, 0x38, 0x14, 0x38, 0x50, 0x38, 0x10, 0x00, 
  117. 0x00, 0x00, 0x48, 0x54, 0x28, 0x10, 0x28, 0x54, 0x24, 0x00, 
  118. 0x00, 0x00, 0x34, 0x48, 0x54, 0x20, 0x50, 0x50, 0x20, 0x00, 
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x18, 0x00, 
  120. 0x00, 0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, 
  121. 0x00, 0x00, 0x20, 0x10, 0x08, 0x08, 0x08, 0x10, 0x20, 0x00, 
  122. 0x00, 0x00, 0x00, 0x44, 0x28, 0x7c, 0x28, 0x44, 0x00, 0x00, 
  123. 0x00, 0x00, 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00, 0x00, 
  124. 0x00, 0x20, 0x10, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  126. 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  127. 0x00, 0x00, 0x40, 0x40, 0x20, 0x10, 0x08, 0x04, 0x04, 0x00, 
  128. 0x00, 0x00, 0x10, 0x28, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00, 
  129. 0x00, 0x00, 0x7c, 0x10, 0x10, 0x10, 0x50, 0x30, 0x10, 0x00, 
  130. 0x00, 0x00, 0x7c, 0x40, 0x20, 0x18, 0x04, 0x44, 0x38, 0x00, 
  131. 0x00, 0x00, 0x38, 0x44, 0x04, 0x18, 0x08, 0x04, 0x7c, 0x00, 
  132. 0x00, 0x00, 0x08, 0x08, 0x7c, 0x48, 0x28, 0x18, 0x08, 0x00, 
  133. 0x00, 0x00, 0x38, 0x44, 0x04, 0x64, 0x58, 0x40, 0x7c, 0x00, 
  134. 0x00, 0x00, 0x38, 0x44, 0x64, 0x58, 0x40, 0x20, 0x18, 0x00, 
  135. 0x00, 0x00, 0x20, 0x20, 0x10, 0x08, 0x08, 0x04, 0x7c, 0x00, 
  136. 0x00, 0x00, 0x38, 0x44, 0x44, 0x38, 0x44, 0x44, 0x38, 0x00, 
  137. 0x00, 0x00, 0x30, 0x08, 0x04, 0x34, 0x4c, 0x44, 0x38, 0x00, 
  138. 0x00, 0x10, 0x38, 0x10, 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 
  139. 0x00, 0x20, 0x10, 0x18, 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 
  140. 0x00, 0x00, 0x04, 0x08, 0x10, 0x20, 0x10, 0x08, 0x04, 0x00, 
  141. 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x00, 0x00, 
  142. 0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40, 0x00, 
  143. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x08, 0x44, 0x38, 0x00, 
  144. 0x00, 0x00, 0x38, 0x40, 0x58, 0x54, 0x4c, 0x44, 0x38, 0x00, 
  145. 0x00, 0x00, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x28, 0x10, 0x00, 
  146. 0x00, 0x00, 0x78, 0x24, 0x24, 0x38, 0x24, 0x24, 0x78, 0x00, 
  147. 0x00, 0x00, 0x38, 0x44, 0x40, 0x40, 0x40, 0x44, 0x38, 0x00, 
  148. 0x00, 0x00, 0x78, 0x24, 0x24, 0x24, 0x24, 0x24, 0x78, 0x00, 
  149. 0x00, 0x00, 0x7c, 0x40, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00, 
  150. 0x00, 0x00, 0x40, 0x40, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00, 
  151. 0x00, 0x00, 0x38, 0x44, 0x4c, 0x40, 0x40, 0x44, 0x38, 0x00, 
  152. 0x00, 0x00, 0x44, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x44, 0x00, 
  153. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00, 
  154. 0x00, 0x00, 0x38, 0x44, 0x04, 0x04, 0x04, 0x04, 0x1c, 0x00, 
  155. 0x00, 0x00, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44, 0x00, 
  156. 0x00, 0x00, 0x7c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 
  157. 0x00, 0x00, 0x44, 0x44, 0x44, 0x54, 0x6c, 0x44, 0x44, 0x00, 
  158. 0x00, 0x00, 0x44, 0x44, 0x4c, 0x54, 0x64, 0x44, 0x44, 0x00, 
  159. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 
  160. 0x00, 0x00, 0x40, 0x40, 0x40, 0x78, 0x44, 0x44, 0x78, 0x00, 
  161. 0x00, 0x04, 0x38, 0x54, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 
  162. 0x00, 0x00, 0x44, 0x48, 0x50, 0x78, 0x44, 0x44, 0x78, 0x00, 
  163. 0x00, 0x00, 0x38, 0x44, 0x04, 0x38, 0x40, 0x44, 0x38, 0x00, 
  164. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00, 
  165. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x00, 
  166. 0x00, 0x00, 0x10, 0x28, 0x28, 0x28, 0x44, 0x44, 0x44, 0x00, 
  167. 0x00, 0x00, 0x44, 0x6c, 0x54, 0x54, 0x44, 0x44, 0x44, 0x00, 
  168. 0x00, 0x00, 0x44, 0x44, 0x28, 0x10, 0x28, 0x44, 0x44, 0x00, 
  169. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x28, 0x44, 0x44, 0x00, 
  170. 0x00, 0x00, 0x7c, 0x40, 0x20, 0x10, 0x08, 0x04, 0x7c, 0x00, 
  171. 0x00, 0x00, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, 0x00, 
  172. 0x00, 0x00, 0x04, 0x04, 0x08, 0x10, 0x20, 0x40, 0x40, 0x00, 
  173. 0x00, 0x00, 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x28, 0x10, 0x00, 
  175. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x30, 0x00, 
  177. 0x00, 0x00, 0x3c, 0x44, 0x3c, 0x04, 0x38, 0x00, 0x00, 0x00, 
  178. 0x00, 0x00, 0x58, 0x64, 0x44, 0x64, 0x58, 0x40, 0x40, 0x00, 
  179. 0x00, 0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x00, 0x00, 0x00, 
  180. 0x00, 0x00, 0x34, 0x4c, 0x44, 0x4c, 0x34, 0x04, 0x04, 0x00, 
  181. 0x00, 0x00, 0x38, 0x40, 0x7c, 0x44, 0x38, 0x00, 0x00, 0x00, 
  182. 0x00, 0x00, 0x20, 0x20, 0x20, 0x78, 0x20, 0x24, 0x18, 0x00, 
  183. 0x38, 0x44, 0x38, 0x40, 0x30, 0x48, 0x34, 0x00, 0x00, 0x00, 
  184. 0x00, 0x00, 0x44, 0x44, 0x44, 0x64, 0x58, 0x40, 0x40, 0x00, 
  185. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x30, 0x00, 0x10, 0x00, 
  186. 0x30, 0x48, 0x48, 0x08, 0x08, 0x08, 0x18, 0x00, 0x08, 0x00, 
  187. 0x00, 0x00, 0x44, 0x48, 0x70, 0x48, 0x44, 0x40, 0x40, 0x00, 
  188. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x30, 0x00, 
  189. 0x00, 0x00, 0x44, 0x54, 0x54, 0x54, 0x68, 0x00, 0x00, 0x00, 
  190. 0x00, 0x00, 0x44, 0x44, 0x44, 0x64, 0x58, 0x00, 0x00, 0x00, 
  191. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00, 
  192. 0x40, 0x40, 0x40, 0x58, 0x64, 0x64, 0x58, 0x00, 0x00, 0x00, 
  193. 0x04, 0x04, 0x04, 0x34, 0x4c, 0x4c, 0x34, 0x00, 0x00, 0x00, 
  194. 0x00, 0x00, 0x40, 0x40, 0x40, 0x64, 0x58, 0x00, 0x00, 0x00, 
  195. 0x00, 0x00, 0x78, 0x04, 0x38, 0x40, 0x38, 0x00, 0x00, 0x00, 
  196. 0x00, 0x00, 0x18, 0x24, 0x20, 0x20, 0x78, 0x20, 0x20, 0x00, 
  197. 0x00, 0x00, 0x34, 0x4c, 0x44, 0x44, 0x44, 0x00, 0x00, 0x00, 
  198. 0x00, 0x00, 0x10, 0x28, 0x28, 0x44, 0x44, 0x00, 0x00, 0x00, 
  199. 0x00, 0x00, 0x28, 0x54, 0x54, 0x44, 0x44, 0x00, 0x00, 0x00, 
  200. 0x00, 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00, 0x00, 
  201. 0x38, 0x44, 0x04, 0x34, 0x4c, 0x44, 0x44, 0x00, 0x00, 0x00, 
  202. 0x00, 0x00, 0x7c, 0x20, 0x10, 0x08, 0x7c, 0x00, 0x00, 0x00, 
  203. 0x00, 0x00, 0x0c, 0x10, 0x08, 0x30, 0x08, 0x10, 0x0c, 0x00, 
  204. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 
  205. 0x00, 0x00, 0x60, 0x10, 0x20, 0x18, 0x20, 0x10, 0x60, 0x00, 
  206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x54, 0x24, 0x00, 
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  208. };
  209.  
  210. int fontbase = 256;
  211.  
  212. #define MAX_COLORS 15
  213.  
  214. float colors[MAX_COLORS][3] = {
  215.     { 1.0, 0.0, 0.0 },
  216.     { 0.8, 0.2, 0.0 },
  217.     { 0.6, 0.4, 0.0 },
  218.     { 0.4, 0.6, 0.0 },
  219.     { 0.2, 0.8, 0.0 },
  220.     { 0.0, 1.0, 0.0 },
  221.     { 0.0, 0.8, 0.2 },
  222.     { 0.0, 0.6, 0.4 },
  223.     { 0.0, 0.4, 0.6 },
  224.     { 0.0, 0.2, 0.8 },
  225.     { 0.0, 0.0, 1.0 },
  226.     { 0.2, 0.0, 0.8 },
  227.     { 0.4, 0.0, 0.6 },
  228.     { 0.6, 0.0, 0.4 },
  229.     { 0.8, 0.0, 0.2 },
  230. };
  231.  
  232. void initializeFont(void)
  233. {
  234.     int i;
  235.  
  236.     glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  237.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  238.  
  239.     for (i=0; i<128; i++) {
  240.     glNewList(i+fontbase, GL_COMPILE);
  241.     glBitmap(6, 10, 0.0, 0.0, 6.0, 0.0, font6x10+10*i);
  242.     glEndList();
  243.     }
  244. }
  245.  
  246. void writeFont(char *string)
  247. {
  248.     glListBase(fontbase);
  249.     glCallLists(strlen(string), GL_BYTE, (unsigned char *) string);
  250. }
  251.  
  252. static void Init(void)
  253. {
  254.     glViewport(0,0,W,H);
  255.     glMatrixMode(GL_PROJECTION);
  256.     glLoadIdentity();
  257.     glOrtho(0,W,0,H,-1,1);
  258.     glMatrixMode(GL_MODELVIEW);
  259.  
  260.     if (rgb) {
  261.     glClearColor(0,0,0,0);
  262.     } else {
  263.     glClearIndex(0);
  264.     }
  265.  
  266.     glClear(GL_COLOR_BUFFER_BIT);
  267.  
  268.     initializeFont();
  269. }
  270.  
  271. static void Redraw(void)
  272. {
  273.     int i, y;
  274.     static int color;
  275.  
  276.     color = (color+1) % MAX_COLORS;
  277.     for (i=0; i<2; i++) {
  278.     if (i == 0) {
  279.         glXMakeCurrent(dpy, window, cx);
  280.     } else {
  281.         glXMakeCurrent(dpy, window2, cx);
  282.     }
  283.  
  284.     if (rgb) {
  285.         glColor3fv(colors[color]);
  286.     } else {
  287.         glIndexi(color);
  288.     }
  289.  
  290.     glClear(GL_COLOR_BUFFER_BIT);
  291.  
  292.     glRasterPos2i(5, 50);
  293.     writeFont("Hello");
  294.     sginap(10);
  295.     glRasterPos2i(5, 40);
  296.     writeFont("There");
  297.     sginap(10);
  298.     glRasterPos2i(5, 30);
  299.     writeFont("World");
  300.  
  301.     if (doubleBuf) {
  302.         if (i == 0) {
  303.         glXSwapBuffers(dpy, window);
  304.         } else {
  305.         glXSwapBuffers(dpy, window2);
  306.         }
  307.     }
  308.     }
  309. }
  310.  
  311. static void Usage(void)
  312. {
  313.     fprintf(stderr, "Usage: wind2 [-c] [-s]\n");
  314.     fprintf(stderr, " -c  run in color index mode\n");
  315.     fprintf(stderr, " -s  run in singlebuffer mode\n");
  316.     exit(1);
  317. }
  318.  
  319. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  320. {
  321.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  322.     return GL_TRUE;
  323.     }
  324.     return GL_FALSE;
  325. }
  326.  
  327. int main(int argc, char** argv)
  328. {
  329.     XVisualInfo *vi;
  330.     Colormap cmap;
  331.     XSetWindowAttributes swa;
  332.     XEvent event;
  333.     XColor white;
  334.     int i;
  335.  
  336.     rgb = 1;
  337.     doubleBuf = 1;
  338.     for (i = 1; i < argc; i++) {
  339.         if (argv[i][0] == '-') {
  340.             switch (argv[i][1]) {
  341.               case 'c':
  342.                 rgb = GL_FALSE;
  343.                 break;
  344.           case 's':
  345.         doubleBuf = 0;
  346.         break;
  347.               default:
  348.                 Usage();
  349.             }
  350.         } else {
  351.             Usage();
  352.         }
  353.     }
  354.  
  355.     dpy = XOpenDisplay(0);
  356.     if (!dpy) {
  357.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  358.     return -1;
  359.     }
  360.  
  361.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  362.              doubleBuf ? 
  363.              (rgb ? RGB_attributes_DB : CI_attributes_DB) :
  364.              (rgb ? RGB_attributes : CI_attributes));
  365.     if (!vi) {
  366.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  367.         getenv("DISPLAY"));
  368.     return -1;
  369.     }
  370.  
  371.     printf("Drawing with one context, two windows.\n");
  372.     if (rgb) {
  373.     printf("rgb mode, ");
  374.     } else {
  375.     printf("color index mode, ");
  376.     }
  377.     if (doubleBuf) {
  378.     printf("double buffered\n");
  379.     } else {
  380.     printf("single buffered\n");
  381.     }
  382.  
  383.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  384.                rgb ? AllocNone : AllocAll);
  385.     if (rgb) {
  386.     white.red = ~0;
  387.     white.green = ~0;
  388.     white.blue = ~0;
  389.     XAllocColor(dpy, cmap, &white);
  390.     swa.background_pixel = white.pixel;
  391.     } else {
  392.     swa.background_pixel = 15;
  393.     }
  394.  
  395.     if (!rgb) {
  396.     XColor buf;
  397.     int i;
  398.  
  399.     buf.flags = DoRed | DoGreen | DoBlue;
  400.  
  401.     /* Init color map */
  402.     for (i=0; i<16; i++) {
  403.         buf.pixel = i;
  404.         buf.red = (i & 4) ? 65535 : 0;
  405.         buf.green = (i & 2) ? 65535 : 0;
  406.         buf.blue = (i & 1) ? 65535 : 0;
  407.         XStoreColor(dpy, cmap, &buf);
  408.     }
  409.     }
  410.  
  411.     swa.border_pixel = 0;
  412.     swa.colormap = cmap;
  413.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  414.     | KeyReleaseMask;
  415.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  416.                W, H,
  417.                0, vi->depth, InputOutput, vi->visual,
  418.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  419.                &swa);
  420.     XSetWMColormapWindows(dpy, window, &window, 1);
  421.     XMapWindow(dpy, window);
  422.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  423.  
  424.     window2 = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  425.                W, H,
  426.                0, vi->depth, InputOutput, vi->visual,
  427.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  428.                &swa);
  429.     XSetWMColormapWindows(dpy, window2, &window2, 1);
  430.     XMapWindow(dpy, window2);
  431.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window2);
  432.  
  433.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  434.     if (!glXMakeCurrent(dpy, window, cx)) {
  435.     fprintf(stderr, "Can't make window current to context\n");
  436.     return -1;
  437.     }
  438.     Init();
  439.  
  440.     for (;;) {
  441.     if (XPending(dpy)) do {
  442.         XNextEvent(dpy, &event);
  443.         switch (event.type) {
  444.           case Expose:
  445.         break;
  446.           case ConfigureNotify:
  447.         W = event.xconfigure.width;
  448.         H = event.xconfigure.height;
  449.         glViewport(0,0,W,H);
  450.         glMatrixMode(GL_PROJECTION);
  451.         glLoadIdentity();
  452.         glOrtho(0,W,0,H,-1,1);
  453.         glMatrixMode(GL_MODELVIEW);
  454.         glClear(GL_COLOR_BUFFER_BIT);
  455.         break;
  456.           case KeyPress:
  457.         {
  458.             char buf[100];
  459.             int rv;
  460.             KeySym ks;
  461.  
  462.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  463.             switch (ks) {
  464.               case XK_F: case XK_f:
  465.               case XK_S: case XK_s:
  466.               case XK_P: case XK_p:
  467.               case XK_L: case XK_l:
  468.               case XK_C: case XK_c:
  469.             break;
  470.               case XK_Escape:
  471.             return 0;
  472.               default:
  473.             break;
  474.             }
  475.         }
  476.         break;
  477.         }
  478.     } while (XPending(dpy) != 0);
  479.  
  480.     Redraw();
  481.     }
  482. }
  483.